package top.infopub.mgr.mkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.CommonCreditDetailsDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.dao.BusinessDetailsDao;
import top.infopub.mgr.mkt.dao.CreditDetailsDtoDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.BusinessDetailsService;
import top.infopub.model.mkt.*;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Warking
 * @Date 2021/5/6 21:08
 * @Version 1.0
 */
@Service
public class BusinessDetailsServiceImpl extends GenericFlowController implements BusinessDetailsService {

    //详情复用 资信标的
    @Autowired
    private CreditDetailsDtoServiceImpl creditDetailsDtoService;
    @Autowired
    private BusinessDetailsDao businessDetailsDao;
    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private CollectProjectServiceImpl collectProjectService;
    @Autowired
    private CreditDetailsDtoDao creditDetailsDtoDao;


    @Override
    public CommonCreditDetailsDto queryDetail(String proCode) {
        CommonCreditDetailsDto dto = creditDetailsDtoService.commonTender(new CommonCreditDetailsDto(), proCode,"2");
        ProjectInfo projectInfo = dto.getProjectInfo();
        //查询页面经济分析的数据
        Map<String,Object>  map = businessDetailsDao.selectReplenishProjectInfo(proCode);
        System.out.println(map);
        projectInfo.setStockPercent((BigDecimal) map.get("STOCKPERCENT"));
        projectInfo.setCapitalFund((BigDecimal) map.get("CAPITALFUND"));
        projectInfo.setJianAnDownRate((BigDecimal) map.get("JIANANDOWNRATE"));
        projectInfo.setProfitRate((BigDecimal) map.get("PROFITRATE"));
        projectInfo.setCompreRate((BigDecimal) map.get("COMPRERATE"));
        projectInfo.setYearDiscountRate((BigDecimal) map.get("YEARDISCOUNTRATE"));
        projectInfo.setBuildConstrRate((BigDecimal) map.get("BUILDCONSTRRATE"));
        projectInfo.setInvestBackRate((BigDecimal) map.get("INVESTPROFITRATE"));
        projectInfo.setOperatProfit((BigDecimal) map.get("OPERATPROFIT"));
        projectInfo.setComperProfit((BigDecimal) map.get("COMPERPROFIT"));
        //查询商务标的数据
        List<BusinessDetails> businessDetailsList = businessDetailsDao.businessDetail(proCode);
        dto.setBusinessDetails(businessDetailsList);
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE25.getCode(), null);
        dto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));
        return dto;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(CommonCreditDetailsDto dto, String proCode) {
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //修改 标书评审的数据
        BidReview bidReview = dto.getBidReview();
        if (bidReview.getInvestEndTime() != null || bidReview.getWillApplyMeetTime() != null || (bidReview.getProManagers() != null && bidReview.getProManagers() != "")) {
            /*Review需要有项目全局唯一编码 */
            bidReview.setProCode(proCode);
            creditDetailsDtoDao.updateReview(bidReview);
        }
        //修改和保存商务标数据
        List<BusinessDetails> businessDetailsList = dto.getBusinessDetails();
        if (businessDetailsList != null && businessDetailsList.size() > 0) {
            for (int i = 0; i < businessDetailsList.size(); i++) {
                BusinessDetails businessDetails = businessDetailsList.get(i);
                if (businessDetails.getId() != null && businessDetails.getId() != "") {
                    //修改
                    businessDetails.setUptBy(vo.getId());
                    businessDetailsDao.updateSingle(businessDetails);

                } else {
                    //保存
                    businessDetails.setAddBy(vo.getId());
                    businessDetails.setUptBy(vo.getId());
                    businessDetails.setProCode(proCode);
                    businessDetails.setBidEvalId(bidReview.getId());
                    businessDetailsDao.insertSingle(businessDetails);
                }
            }//for
        }// if


        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if (dto.getFileAtt() != null) {
            String[] fileAtt = dto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, proCode, file);
                attachmentList.add(attachment);
            }
        }
        //更新附件信息
        projectAttachmentDao.deleteByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE25.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        return 1;
    }
    @Override
    public int deleteBusinessDetail(String sguid) {
        return businessDetailsDao.deleteBusinessDetail(sguid);

    }

}
